package com.itstu.service.impl;

import com.itstu.exception.GlobalException;
import com.itstu.mapper.UserMapper;
import com.itstu.pojo.User;
import com.itstu.service.LoginService;
import com.itstu.utils.JwtUtil;
import com.itstu.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName LoginService
 * @Description TODO
 * @Date 2024/09/27 22:02
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private JwtUtil jwtUtil;

    /**
     * 登录
     * @param user 登录参数
     */
    @Override
    public String login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        // 查询用户信息
        User userDao = userMapper.getUserByUserName(username);
        if (ObjectUtils.isEmpty(userDao)) {
            throw new GlobalException("用户不存在");
        }
        if (!userDao.getPassword().equals(password)) {
            throw new GlobalException("密码错误");
        }
        return jwtUtil.createJWT(userDao);
    }
}
